import { createRouter, createWebHistory } from "vue-router";

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    // {
    //   path: "/",
    //   redirect: "/home",
    // },
    {
      path: "/",
      component: () => import("../pages/home/Index.vue"),
      children: [
        {
          path: "/",
          component: () => import("../pages/home/homeIndex/Index.vue"),
        },
        {
          path: "/banner/list",
          component: () => import("../pages/home/bannerList/Index.vue"),
        },
        {
          path: "/banner/add",
          component: () => import("../pages/home/bannerAdd/Index.vue"),
        },
        {
          path: "/pro/list",
          component: () => import("../pages/home/proList/Index.vue"),
        },
        {
          path: "/data/echarts",
          component: () => import("../pages/home/dataEcharts/Index.vue"),
        },
        {
          path: "/map/index",
          component: () => import("../pages/home/mapIndex/Index.vue"),
        },
        {
          path: "/editor/index",
          component: () => import("../pages/home/editorIndex/Index.vue"),
        },
        {
          path: "/editor/md",
          component: () => import("../pages/home/editorMd/Index.vue"),
        },
        {
          path: "/excel/export",
          component: () => import("../pages/home/excelExport/Index.vue"),
        },
        {
          path: "/excel/import",
          component: () => import("../pages/home/excelImport/Index.vue"),
        },
        {
          path: "/user/admin",
          component: () => import("../pages/home/userAdmin/Index.vue"),
        },
      ],
    },
    {
      path: "/login",
      component: () => import("../pages/login/Index.vue"),
    },
    {
      path: "/:pathMatch(.*)*",
      component: () => import("../pages/notfound/Index.vue"),
    },
  ],
});

router.beforeEach((to, from, next) => {
  if (to.path !== "/login") {
    if (localStorage.getItem("token")) {
      next();
    } else {
      next("/login");
    }
  } else {
    next();
  }
});

export default router;
